package com.yxmadoc.umlproject.controller;

import com.alibaba.fastjson.JSONObject;
import com.yxmadoc.umlproject.config.enums.ReturnResult;
import com.yxmadoc.umlproject.model.Customer;
import com.yxmadoc.umlproject.model.Repairer;
import com.yxmadoc.umlproject.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public JSONObject login(@RequestBody JSONObject jsonObject){
        JSONObject result = new JSONObject();
        Integer userType = jsonObject.getInteger("userType");
        switch(userType){
            case 0:
                Customer customer = jsonObject.getObject("user", Customer.class);
                result.put(ReturnResult.STATUS.getCode(), loginService.login(customer));
                result.put("token", loginService.getToken(customer));
                break;
            case 1:
                Repairer repairer = jsonObject.getObject("user", Repairer.class);
                result.put(ReturnResult.STATUS.getCode(), loginService.login(repairer));
                result.put("token", loginService.getToken(repairer));
                break;
        }
        return result;
    }

}
